package com.hcinfos.gsoo.adapter;

import java.text.ParseException;
import java.util.List;

import com.hcinfos.gsoo.R;
import com.hcinfos.gsoo.config.Constants;
import com.hcinfos.gsoo.enity.GdTakeAwayOrder;
import com.hcinfos.gsoo.interfaze.DataCallback;
import com.hcinfos.gsoo.interfaze.OrderCallback;
import com.hcinfos.gsoo.util.DateUtil;
import com.hcinfos.gsoo.util.LogUtil;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

public class MyDingzuoAdapter extends BaseAdapter {

	private List<GdTakeAwayOrder> list;
	private LayoutInflater lif;
	private OrderCallback callback;

	public MyDingzuoAdapter(Context context, List<GdTakeAwayOrder> list) {

		this.list = list;
		lif = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		Holder holder;
		if (convertView == null) {
			holder = new Holder();
			convertView = lif.inflate(R.layout.adapter_my_dingzuo_list_item,
					null);
			convertView.setTag(holder);
			holder.time_tv = (TextView) convertView.findViewById(R.id.time_tv);
			holder.name_tv = (TextView) convertView.findViewById(R.id.name_tv);
			holder.moneyNum_tv = (TextView) convertView
					.findViewById(R.id.money_num_tv);
			holder.submit_bt = (Button) convertView
					.findViewById(R.id.submit_bt);
			holder.cancel_bt = (Button) convertView
					.findViewById(R.id.cancel_bt);
			holder.cancel_pay_bt = (Button) convertView
					.findViewById(R.id.cancel_pay_bt);
			holder.reason_tv = (TextView) convertView
					.findViewById(R.id.reason_tv);
			holder.orderNum_tv = (TextView) convertView
					.findViewById(R.id.orderNum_tv);
			holder.queren_bt = (Button) convertView
					.findViewById(R.id.queren_bt);
			holder.content_tv = (TextView) convertView
					.findViewById(R.id.content_tv);
			holder.orderName_tv = (TextView) convertView
					.findViewById(R.id.orderName_tv);
			holder.num_tv = (TextView) convertView.findViewById(R.id.num_tv);
			holder.orderTime_tv = (TextView) convertView
					.findViewById(R.id.orderTime_tv);

		} else {
			holder = (Holder) convertView.getTag();

		}
		GdTakeAwayOrder order = list.get(position);
		// + order.getOrderState()
		holder.name_tv.setText(order.getSuppNick());
		holder.moneyNum_tv.setText("￥" + order.getTotalPrice());
		holder.time_tv.setText("点餐时间：" + order.getAddDatetime());
		holder.orderNum_tv.setText("订单号:" + order.getId());
		final String orderId = order.getId();
		holder.cancel_pay_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				if (callback != null)
					callback.callback(0, orderId);// 申请退款
			}
		});
		holder.cancel_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (callback != null)
					callback.callback(1, orderId);// 取消订单
			}
		});
		holder.submit_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (callback != null)
					callback.callback(2, orderId);// 支付订单
			}
		});
		holder.queren_bt.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (callback != null)
					callback.callback(3, orderId);// 支付订单
			}
		});
		int days = 0;
		try {
			days = DateUtil.daysBetween(DateUtil.getCurrentTime(),
					order.getAddDatetime());
			LogUtil.d("wxf", "days=" + days);
		} catch (ParseException e) {
			days = 0;
		}
		if (Math.abs(days) < 1) {
			switch (order.getOrderState()) {
			case Constants.ORDER_STATE_WAIT_USER_CHECK:
				// 待用户确认 (确认后钱才会打到商家账户)
				holder.queren_bt.setVisibility(View.VISIBLE);
				holder.reason_tv.setVisibility(View.GONE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				break;
			case Constants.ORDER_STATE_DEAL:
				// 交易成功
				holder.reason_tv.setText("交易成功");
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.queren_bt.setVisibility(View.GONE);
				break;
			case Constants.ORDER_STATE_PAY_ERROR:
				holder.reason_tv.setText("订单异常");
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.queren_bt.setVisibility(View.GONE);
				break;
			case Constants.ORDER_STATE_SUPP_CANCEL:
				// 已取消
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("已取消");
				holder.queren_bt.setVisibility(View.GONE);

				break;
			case Constants.ORDER_STATE_USER_CANCEL:
				// 自己取消
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("正在取消中");
				holder.queren_bt.setVisibility(View.GONE);

				break;

			case Constants.ORDER_STATE_USER_REFUND:
				// 退款申请中
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("退款中");
				holder.queren_bt.setVisibility(View.GONE);

				break;

			case Constants.ORDER_STATE_TOUT:
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("订单失效");
				holder.queren_bt.setVisibility(View.GONE);
				// 订单失肖
				break;
			case Constants.ORDER_STATE_CLOSE:
				// 订单关闭
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("订单已关闭");
				holder.queren_bt.setVisibility(View.GONE);
				break;
			case Constants.ORDER_STATE_WAIT_ONLINE_PAY:
				// 等待在线支付
				holder.reason_tv.setVisibility(View.GONE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.VISIBLE);
				holder.submit_bt.setVisibility(View.VISIBLE);
				holder.queren_bt.setVisibility(View.GONE);

				break;
			case Constants.ORDER_STATE_WAIT_DELIVERY:
				// 等待派送
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("等待确认");
				holder.queren_bt.setVisibility(View.GONE);

				break;
			case Constants.ORDER_STATE_DELIVERYING:
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("订座成功");
				holder.queren_bt.setVisibility(View.GONE);

				// 真在派送
				break;
			case Constants.ORDER_STATE_SUCCESS_DELIVERY:
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("已上桌");
				holder.queren_bt.setVisibility(View.GONE);

				// 派送成功
				break;
			case Constants.ORDER_PAY_FACE:
				holder.reason_tv.setVisibility(View.GONE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.VISIBLE);
				holder.submit_bt.setVisibility(View.VISIBLE);
				holder.queren_bt.setVisibility(View.GONE);
				// 等待在线支付
				break;
			case Constants.ORDER_PAY_ONLINE_SUCCESS:
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("支付成功");
				holder.queren_bt.setVisibility(View.GONE);
				// 在线支付成功
				break;

			case Constants.ORDER_PAY_ONLINE_FAIL:
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("支付失败");
				holder.queren_bt.setVisibility(View.GONE);

				// 在线支付失败
				break;
			case Constants.ORDER_PAY_REFUND_SUCCESS:
				// 已退款
				holder.reason_tv.setVisibility(View.VISIBLE);
				holder.cancel_pay_bt.setVisibility(View.GONE);
				holder.cancel_bt.setVisibility(View.GONE);
				holder.submit_bt.setVisibility(View.GONE);
				holder.reason_tv.setText("已退款");
				holder.queren_bt.setVisibility(View.GONE);
				break;

			}
		} else {
			holder.reason_tv.setVisibility(View.VISIBLE);
			holder.cancel_pay_bt.setVisibility(View.GONE);
			holder.cancel_bt.setVisibility(View.GONE);
			holder.submit_bt.setVisibility(View.GONE);
			holder.reason_tv.setText("已过期");
		}

		holder.content_tv.setText(order.getOrderDes());
		holder.orderTime_tv.setText("到店时间：" + order.getOrderDatetime());
		holder.num_tv.setText("预约人数: " + order.getPersonSum());
		holder.orderName_tv.setText("预约人：" + order.getUserNick());
		return convertView;
	}

	public final class Holder {

		TextView time_tv;
		TextView name_tv;
		TextView moneyNum_tv;
		Button submit_bt;
		Button cancel_bt;
		Button cancel_pay_bt;
		TextView reason_tv;
		TextView orderNum_tv;
		Button queren_bt;
		TextView content_tv;
		TextView orderName_tv;
		TextView num_tv;
		TextView orderTime_tv;

	}

	public void setDataChange(List<GdTakeAwayOrder> list) {
		this.list = list;
		notifyDataSetChanged();
	}

	public void setCallback(OrderCallback call) {
		this.callback = call;
	}
}
